import * as request from '../constant/fetch-request'
import HTTP_CODE from '../constant/http-code'
import { REFRESH_SERIALS_APPLICATIONS_PAGEABLE } from '../constant/action-type'
import {message} from 'antd'
const refreshSerialsApplicationsPageable = data => ({
  type: REFRESH_SERIALS_APPLICATIONS_PAGEABLE,
  data
})

export const apply = (application, callback) => {
  return () => {
    (async () => {
      const res = await request.post(`./api/applications`, application)
      if (res.status === HTTP_CODE.CREATED) {
        callback()
      }
    })()
  }
}

export const getMySerialsApplications = (current, status) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`./api/applications?current=${current}&status=${status}`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshSerialsApplicationsPageable(res.body))
      }
    })()
  }
}

export const getApplications = (current = 1) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`./api/applications/users?current=${current}`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshSerialsApplicationsPageable(res.body))
      }
    })()
  }
}

export const handApplication = (id, status, callback) => {
  return () => {
    (async () => {
      const res = await request.update(`./api/applications/${id}/handling?status=${status}`)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        message.success('处理成功')
        callback()
      }
    })()
  }
}
